package com.nengchuan.kbms.dfs.controller.work;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nengchuan.excel.util.CsncExcelUtil;
import com.nengchuan.kbms.dfs.entity.dto.DataReceivePageDTO;
import com.nengchuan.kbms.dfs.entity.dto.DataSharePageDTO;
import com.nengchuan.kbms.dfs.entity.vo.DataReceiveVO;
import com.nengchuan.kbms.dfs.entity.vo.DataShareVO;
import com.nengchuan.kbms.dfs.service.DataShareService;
import com.nengchuan.model.model.NcPageInfo;
import com.nengchuan.model.model.WrappedResult;
import com.nengchuan.util.MapperUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 文件分享 前端控制器
 * </p>
 *
 * @author abe
 * @since 2020-03-31
 */
@RestController
@RequestMapping("/work/share")
@Api(tags = "知识库数据后台管理 > 工作管理 > 我的分享、分享给我 【已完成】")
public class ShareController {

    @Autowired
    private DataShareService dataShareService;

    @PostMapping("/page")
    @ApiOperation(value = "我的分享")
    public WrappedResult<NcPageInfo<DataShareVO>> page(@RequestBody DataSharePageDTO dataSharePageDTO) {
        IPage<DataShareVO> pageList = dataShareService.sharePage(dataSharePageDTO);
        return WrappedResult.success(MapperUtils.mappingNcPage(pageList, DataShareVO.class));
    }

    @PostMapping(value = "/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "我的分享导出列表")
    public void export(@RequestBody DataSharePageDTO pageDTO, HttpServletResponse response) throws Exception {
        List<DataShareVO> list = dataShareService.list(pageDTO, DataShareVO.class);
        CsncExcelUtil.export(list, DataShareVO.class, response, "我的分享列表.xls");
    }

    @PostMapping("/receive/page")
    @ApiOperation(value = "分享给我")
    public WrappedResult<NcPageInfo<DataReceiveVO>> receive(@RequestBody DataReceivePageDTO dataReceivePageDTO) {
        IPage<DataReceiveVO> pageList = dataShareService.receivePage(dataReceivePageDTO);
        return WrappedResult.success(MapperUtils.mappingNcPage(pageList, DataReceiveVO.class));
    }

    @PostMapping(value = "/receive/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    @ApiOperation(value = "分享给我导出列表")
    public void receiveExport(@RequestBody DataReceivePageDTO dataReceivePageDTO, HttpServletResponse response) throws Exception {
        dataReceivePageDTO.setPageNum(1);
        dataReceivePageDTO.setPageSize((int) Short.MAX_VALUE);
        IPage<DataReceiveVO> pageList = dataShareService.receivePage(dataReceivePageDTO);
        CsncExcelUtil.export(pageList.getRecords(), DataReceiveVO.class, response, "分享给我列表.xls");
    }

}
